﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib.GSMModem;
using WFNetLib.Net;
using System.Net;
using System.Diagnostics;
using WFNetLib;

namespace 自消费
{
    public partial class TaskThread
    {
        string 主题_processurl(string url)
        {
            url = url.Replace("%3A", ":");
            url = url.Replace("%2F", "/");
            url = url.Replace("%3F", "?");
            url = url.Replace("%3D", "=");
            url = url.Replace("%26", "&");
            return url;
        }
        void 主题_FinishPer(object sender, strEventArgs e)
        {
            OnAddLog("下载进度:"+e.strArg+"%", (int)TaskThreadEventType.Log);
        }
        void 主题(TaskData td)
        {
            GsmModem gsmModem = new GsmModem(Com);
            gsmModem.dIP[0] = 10;
            gsmModem.dIP[1] = 0;
            gsmModem.dIP[2] = 0;
            gsmModem.dIP[3] = 172;
            gsmModem.dPort = 80;
            gsmModem.ProcessURL = new GsmModem.delegateProcessURL(主题_processurl);
            gsmModem.IPFinishPerEvent += new WFNetLib.strEventHandler(主题_FinishPer);
#if CommunicationDebug
            gsmModem.pppProcLogEvent += new WFNetLib.intstrEventHandler(gsmModemLogEvent);
            gsmModem.IPProcLogEvent += new WFNetLib.intstrEventHandler(gsmModemLogEvent);
#endif
            int Retry = 10;
            try
            {
                while (Retry > 0)
                {
                    Retry--;
                    if (!InitWapTask())
                        continue;
                    OnAddLog("开始拨号", (int)TaskThreadEventType.Log);
                    if (!gsmModem.InitPPPConnect())
                    {
                        OnAddLog("拨号失败", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    //OnAddLog("开始获取订购页面", (int)TaskThreadEventType.Log);
                    break;
                }
                if (Retry < 1)
                    return;
                Retry = 10;
                while (Retry >0)
                {
                    Retry--; 
                    OnAddLog("开始获取订购页面", (int)TaskThreadEventType.Log);
                    object o;
                    string str = "";
                    string strLink;
                    string post;
                    string name;
                    string value;
                    string ua;
                    string wapLink;
                    string[] pa = td.content.Split(new string[1] { "||" }, StringSplitOptions.None);
                    Single finishper;
                    wapLink = pa[2];
                    ua = pa[3];
                    finishper = Convert.ToSingle(pa[4]);
                    Http http = null;
                    gsmModem.finishper = 100;
                    gsmModem.httpHeader = new WebHeaderCollection();
                    gsmModem.httpHeader[HttpRequestHeader.UserAgent] = ua;
                    gsmModem.httpHeader[HttpRequestHeader.Accept] ="*/*";
                    //gsmModem.httpHeader[HttpRequestHeader.AcceptEncoding] = "utf-8";
                    //gsmModem.httpHeader[HttpRequestHeader.AcceptLanguage] = "zh-CN, en-US";
                    gsmModem.url = wapLink;
                    gsmModem.cookies = new CookieCollection();
                    if (!gsmModem.TCPRead())
                    {
                        OnAddLog("获得订购页面失败", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    o = gsmModem.TCPData.oTCPDatas;
                    if (o == null)
                    {
                        OnAddLog("获得订购页面失败", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    http = (Http)o;
                    if (http.ContentDatas.Count == 0)
                    {
                        OnAddLog("获得订购页面失败", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    str = HttpStatic.ContentToStr(http);
                    AddExLog(http.strHttpHeader);
                    AddExLog(str);
                    int nCom = 0, nCom1 = 0;
                    while (true)
                    {
                        nCom = str.IndexOf("<a href=\"", nCom);
                        if (nCom == -1)
                        {
                            break;
                        }
                        nCom1 = str.IndexOf("</a>", nCom);
                        if (nCom1 == -1)
                        {
                            break;
                        }
                        if (str.IndexOf("订购", nCom, nCom1 - nCom) == -1)
                        {
                            nCom = nCom1 + "</a>".Length;
                        }
                        else
                            break;
                    }
                    if (nCom == -1 || nCom1 == -1)
                    {
                        OnAddLog("获得订购链接失败", (int)TaskThreadEventType.Log);
                        nCom = 0;
                        nCom1 = 0;
                        while (true)
                        {
                            nCom = str.IndexOf("<a href=\"", nCom);
                            if (nCom == -1)
                            {
                                break;
                            }
                            nCom1 = str.IndexOf("</a>", nCom);
                            if (nCom1 == -1)
                            {
                                break;
                            }
                            if (str.IndexOf("下载", nCom, nCom1 - nCom) == -1)
                            {
                                nCom = nCom1 + "</a>".Length;
                            }
                            else
                                break;
                        }
                        if (nCom == -1 || nCom1 == -1)
                        {
                            //OnAddLog("获得订购链接失败", (int)TaskThreadEventType.Log);
                            if (str.IndexOf("适用于") != -1 && str.IndexOf("更改") != -1)
                            {
                                OnAddLog("ua不正确", (int)TaskThreadEventType.Error);
                                return;
                            }
                        }
                        nCom = 0;
                        nCom1 = 0;
                        while (true)
                        {
                            nCom = str.IndexOf("<a href=\"", nCom);
                            if (nCom == -1)
                            {
                                break;
                            }
                            nCom1 = str.IndexOf("</a>", nCom);
                            if (nCom1 == -1)
                            {
                                break;
                            }
                            if (str.IndexOf("下载", nCom, nCom1 - nCom) == -1)
                            {
                                nCom = nCom1 + "<a href=\"".Length;
                            }
                            else
                                break;
                        }
                        if (nCom == -1 || nCom1 == -1)
                        {
                            OnAddLog("未找到下载链接", (int)TaskThreadEventType.Error);
                            continue;
                        }
                        nCom = nCom + "<a href=\"".Length;
                        strLink = str.Substring(nCom, str.IndexOf('"', nCom) - nCom);
                        strLink = strLink.Replace("../", "");
                        gsmModem.url = "http://211.138.238.8/wap/" + strLink;
                        gsmModem.finishper = finishper;
                        OnAddLog("开始下载", (int)TaskThreadEventType.Log);
                        if (!gsmModem.TCPRead())
                        {
                            OnAddLog("获得下载数据失败!", (int)TaskThreadEventType.Error);
                            continue;
                        }
                        o = gsmModem.TCPData.oTCPDatas;
                        if (o == null)
                        {
                            OnAddLog("获得下载数据失败!", (int)TaskThreadEventType.Error);
                            continue;
                        }
                        http = (Http)o;
                        if (http.ContentDatas.Count == 0)
                        {
                            OnAddLog("获得下载数据失败!", (int)TaskThreadEventType.Error);
                            continue;
                        }
                        //成功
                        //OnAddLog("关闭网络链接", (int)TaskThreadEventType.Log);                    
                        OnAddLog("下载完成" + td.taskname, (int)TaskThreadEventType.FinishTask);
                        break;
                    }
                    nCom = nCom + "<a href=\"".Length;
                    strLink = str.Substring(nCom, str.IndexOf('"', nCom) - nCom);
                    strLink = strLink.Replace("../", "");
                    gsmModem.url = "http://211.138.238.8/wap/" + strLink;
                    OnAddLog("获得订购页面，开始获得点播链接!", (int)TaskThreadEventType.Log);
                    if (!gsmModem.TCPRead())
                    {
                        OnAddLog("获得点播页面失败!", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    o = gsmModem.TCPData.oTCPDatas;
                    if (o == null)
                    {
                        OnAddLog("获得点播页面失败!", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    http = (Http)o;
                    if (http.ContentDatas.Count == 0)
                    {
                        OnAddLog("获得点播页面失败!", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    str = HttpStatic.ContentToStr(http);
                    AddExLog(http.strHttpHeader);
                    AddExLog(str);
                    nCom = 0;
                    nCom1 = 0;
                    while (true)
                    {
                        nCom = str.IndexOf("<anchor>", nCom);
                        if (nCom == -1)
                        {
                            break;
                        }
                        nCom1 = str.IndexOf("</anchor>", nCom);
                        if (nCom1 == -1)
                        {
                            break;
                        }
                        if (str.IndexOf("我要点播", nCom, nCom1 - nCom) == -1)
                        {
                            nCom = nCom1 + "</anchor>".Length;
                        }
                        else
                            break;
                    }
                    if (nCom == -1 || nCom1 == -1)
                    {
                        OnAddLog("未找到点播链接", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    nCom = str.IndexOf("<go sendreferer=\"true\" href=\"", nCom);
                    if (nCom == -1)
                    {
                        OnAddLog("未找到点播链接", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    nCom = nCom + "<go sendreferer=\"true\" href=\"".Length;
                    strLink = str.Substring(nCom, str.IndexOf('"', nCom) - nCom);
                    strLink = WFNetLib.Strings.StringsFunction.strDecode(strLink);
                    gsmModem.url = strLink;
                    nCom = str.IndexOf("<postfield name=\"", nCom);
                    if (nCom == -1)
                    {
                        OnAddLog("未找到点播链接", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    nCom = nCom + "<postfield name=\"".Length;
                    name = str.Substring(nCom, str.IndexOf('"', nCom) - nCom);
                    nCom = str.IndexOf("value=\"", nCom);
                    if (nCom == -1)
                    {
                        OnAddLog("未找到点播链接", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    OnAddLog("获得点播链接，开始获得点播页面!", (int)TaskThreadEventType.Log);
                    nCom = nCom + "value=\"".Length;
                    value = str.Substring(nCom, str.IndexOf('"', nCom) - nCom);
                    post = "name=" + name + "&value=" + value;
                    Debug.WriteLine("post=" + post);
                    gsmModem.PostTx = new List<byte>(Encoding.ASCII.GetBytes(post));
                    gsmModem.httpHeader[HttpRequestHeader.ContentLength] = post.Length.ToString();
                    gsmModem.httpHeader[HttpRequestHeader.ContentType] = "Application";
                    if (!gsmModem.TCPRead("POST"))
                    {
                        OnAddLog("获得点播页面失败!", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    o = gsmModem.TCPData.oTCPDatas;
                    if (o == null)
                    {
                        OnAddLog("获得点播页面失败!", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    http = (Http)o;
                    if (http.ContentDatas.Count == 0)
                    {
                        OnAddLog("获得点播页面失败!", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    str = HttpStatic.ContentToStr(http);
                    AddExLog(http.strHttpHeader);
                    AddExLog(str);
                    //OnAddLog("获得点播页面，!", (int)TaskThreadEventType.Log);
                    if (str.IndexOf("对不起，因服务提供商") != -1 && str.IndexOf("通讯故障，服务暂时无法完成") != -1)
                    {
                        OnAddLog("扣费成功，暂时无法提供服务" + td.taskname, (int)TaskThreadEventType.FinishTask);
                        //OnAddLog("暂时无法提供服务!", (int)TaskThreadEventType.Error);
                        break;
                    }
                    nCom = 0;
                    nCom1 = 0;
                    while (true)
                    {
                        nCom = str.IndexOf("<a href=\"", nCom);
                        if (nCom == -1)
                        {
                            break;
                        }
                        nCom1 = str.IndexOf("</a>", nCom);
                        if (nCom1 == -1)
                        {
                            break;
                        }
                        if (str.IndexOf("下载", nCom, nCom1 - nCom) == -1)
                        {
                            nCom = nCom1 + "<a href=\"".Length;
                        }
                        else
                            break;
                    }
                    if (nCom == -1 || nCom1 == -1)
                    {
                        OnAddLog("扣费成功，未找到下载链接" + td.taskname, (int)TaskThreadEventType.FinishTask);
                        //OnAddLog("未找到下载链接", (int)TaskThreadEventType.Error);
                        continue;
                    }
                    nCom = nCom + "<a href=\"".Length;
                    strLink = str.Substring(nCom, str.IndexOf('"', nCom) - nCom);
                    strLink = strLink.Replace("../", "");
                    gsmModem.url = strLink;
                    gsmModem.finishper = finishper;
                    OnAddLog("获得点播页面，开始下载", (int)TaskThreadEventType.Log);
                    if (!gsmModem.TCPRead())
                    {
                        OnAddLog("扣费成功，未下载" + td.taskname, (int)TaskThreadEventType.FinishTask);
                        //OnAddLog("获得下载数据失败!", (int)TaskThreadEventType.Error);
                        break;
                        continue;
                    }
                    o = gsmModem.TCPData.oTCPDatas;
                    if (o == null)
                    {
                        OnAddLog("扣费成功，未下载" + td.taskname, (int)TaskThreadEventType.FinishTask);
                        //OnAddLog("获得下载数据失败!", (int)TaskThreadEventType.Error);
                        break;
                        continue;
                    }
                    http = (Http)o;
                    if (http.ContentDatas.Count == 0)
                    {
                        OnAddLog("扣费成功，未下载" + td.taskname, (int)TaskThreadEventType.FinishTask);
                        //OnAddLog("获得下载数据失败!", (int)TaskThreadEventType.Error);
                        break;
                        continue;
                    }
                    str = HttpStatic.ContentToStr(http);
                    AddExLog(http.strHttpHeader);
                    AddExLog(str);
                    //OnAddLog("下载完成", (int)TaskThreadEventType.Log);
                    //完成                
                    OnAddLog("下载完成" + td.taskname, (int)TaskThreadEventType.FinishTask);
                    break;
                }
            }
            catch (InvalidOperationException ex)//端口被关闭,任务结束
            {
                throw ex;
            }
            catch (System.Exception ex)
            {
            	
            }
            finally
            {
                if (gsmModem.bDataTransfer)
                {
                    if (gsmModem.pppClose())
                    {
                        //OnAddLog("关闭网络链接成功", (int)TaskThreadEventType.Log);
                    }
                    else
                    {
                    }
                }
                    //OnAddLog("关闭网络链接成功失败", (int)TaskThreadEventType.Error);
            }
            
        }
    }
}
